package action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;

import model.Position;
import model.User;

import org.apache.struts2.interceptor.RequestAware;

import service.PositionService;
import service.UserService;
import util.DataUtil;
import util.ValidateUtil;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class UserAction extends ActionSupport implements RequestAware,
ModelDriven<User>, Preparable {
	private static final long serialVersionUID = 6137389922322069087L;
	
	private UserService userService;
	private PositionService positionService;
	private InputStream inputStream;
	private Integer id;
	private String email;
	private String repass;
	private Position position;
	
	
	
	public void setPosition(Position position) {
		this.position = position;
	}

	public void setRepass(String repass){
		this.repass = repass;
	}
	
	public void setEmail(String email){
		this.email = email;
	}
	
	public void setId(Integer id){
		this.id = id;
	}
	

	public InputStream getInputStream() {
		return inputStream;
	}
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setPositionService(PositionService positionService) {
		this.positionService = positionService;
	}
	
	/**
	 * 显示注册页面
	 */
	public String input(){
		request.put("position", positionService.getAll());
		return INPUT;
	}
	/**
	 * 注册
	 * @throws UnsupportedEncodingException 
	 */
	public String save() throws UnsupportedEncodingException{
			Position p = new Position();
			model.setRegDate(new Date());			
			model.setPassword(DataUtil.MD5(model.getPassword())); 
			p.setId(4);
			model.setPosition(p);
		userService.saveOrUpdate(model);
		return SUCCESS;
	}
	
	public void prepareSave(){
			model = new User();
	}
	
	/**
	 * 编辑操作
	 */
	public String edit(){
		request.put("position",positionService.getAll());
		return "edit";
	}
	
	public String doEdit(){
		model = userService.get(id);
		//Position p = new Position();
		//p.setId(position.getId());
		model.setPosition(position);
		userService.saveOrUpdate(model);
		return SUCCESS;
	}
	
	public void prepareEdit(){
		model = userService.get(id);
	}

	
	
	
	/**
	 *显示个人空间
	 */
	public String main(){
		
		return "main";
	}
	
	/**
	 * 显示用户列表
	 */
	
	public String list(){
		request.put("user", userService.getAll());
		return "list";
	}
	

	/**
	 * 删除用户
	 */
	
	public String delete(){
		try {
			userService.delete(id);
			inputStream = new ByteArrayInputStream("1".getBytes("UTF-8"));
		} catch (Exception e) {
			e.printStackTrace();
			try {
				inputStream = new ByteArrayInputStream("0".getBytes("UTF-8"));
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
		}
		return "ajax-success";
	}
	
	/**
	 * 验证
	 */
	
	public void validateSave(){
				//1.非空校验
				if(!ValidateUtil.isValid(model.getEmail())){
					addFieldError("email", "邮箱不能为空!");
				}
				if(!ValidateUtil.isValid(model.getPassword())){
					addFieldError("password", "密码不能为空!");
				}
				if(!ValidateUtil.isValid(model.getNickName())){
					addFieldError("nickName", "昵称不能为空!");
				}
				if(hasErrors()){
					return ;
				}
				//2.密码一致性校验
				if(!model.getPassword().equals(repass)){
					addFieldError("password", "密码不一致！");
					return ;
				}
				if(hasErrors()){
					return ;
				}		
				//3.email占用
				if(userService.isRegisted(model.getEmail())){
					addFieldError("email", "该邮箱已占用！");			
				}
	}
	

	@Override
	public void prepare() throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	private User model;
	
	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		return model;
	}

	private Map<String, Object> request;
	
	@Override
	public void setRequest(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		this.request = arg0;
	}

}
